package com.rfsp.mcp.controller.bank;

import com.github.pagehelper.PageInfo;
import com.rfsp.common.base.ResultListVO;
import com.rfsp.common.base.ResultVO;
import com.rfsp.common.interceptor.Token;
import com.rfsp.common.interceptor.TokenType;
import com.rfsp.common.log.TrcMsg;
import com.rfsp.common.shiro.ShiroUtils;
import com.rfsp.common.util.XssUtils;
import com.rfsp.mcp.pojo.dto.FinanceVerifyCancelDTO;
import com.rfsp.mcp.pojo.po.AuthPO;
import com.rfsp.common.pojo.po.OrgPO;
import com.rfsp.common.pojo.po.UserPO;
import com.rfsp.mcp.pojo.sdto.FinanceSDTO;
import com.rfsp.common.pojo.sdto.PageSDTO;
import com.rfsp.common.service.OrgService;
import com.rfsp.mcp.pojo.vo.FinanceVO;
import com.rfsp.mcp.service.AuthService;
import com.rfsp.mcp.service.FinanceService;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@RequiresRoles(value={"mcp_bank"}, logical= Logical.AND)
@Controller("BankFinanceController")
@RequestMapping("/bank/finance")
public class BankFinanceController extends BankController {

    public Logger logger = LoggerFactory.getLogger(BankBargainController.class);

    @Autowired
    FinanceService service;
    
    @Autowired
	AuthService authService;
    
	@Autowired
    OrgService orgService;

    @RequestMapping("/")
    public String index1(ModelMap model, PageSDTO pageSdto, FinanceSDTO sdto) {
    	
        return index(model,pageSdto,sdto);
    }
    @RequestMapping("/index")
    public String index(ModelMap model, PageSDTO pageSdto, FinanceSDTO sdto) {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "BankFinanceController.index"));

        sdto=XssUtils.replaceXss(sdto);

        sdto.setViewRole("bank");
        UserPO currentUser = ShiroUtils.getCurrentUser();
        sdto.setBankOrgId(currentUser.getOrgId());
        
        if(StringUtils.isNotBlank(sdto.getCounty())) {
        	sdto.setAreaCode(sdto.getCounty());
        }else if(StringUtils.isNotBlank(sdto.getCity())) {
        	sdto.setAreaCode(sdto.getCity().substring(0, 4));
        }else if(StringUtils.isNotBlank(sdto.getProvince())) {
        	sdto.setAreaCode(sdto.getProvince().substring(0, 2));
        }

        PageInfo<FinanceVO> page=service.get(sdto,pageSdto);
        service.combo(page.getList());
        ResultListVO<FinanceVO> result = ResultListVO.success(page,sdto);
        model.put("vo", result);

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BankFinanceController.index"));
        return "bank/finance/index";
    }
    /*
    @GetMapping("/detail/")
    public String detail(ModelMap model,Long id) {
        FinanceVO vo=service.getById(id);
        model.put("vo", vo);
        return "bank/finance1/detail";
    }
    */

    @Token
    @GetMapping("/verifyCancel")
    public String verifyCancelForm(ModelMap model,Long id, @ModelAttribute("dto") FinanceVerifyCancelDTO dto,BindingResult bindingResult) {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "BankFinanceController.verifyCancelForm"));

        FinanceVO vo=service.getById(id);
        model.put("vo", vo);
        model.put("dto",dto);

        ResultVO<FinanceVO> result = ResultVO.success(vo);
        model.put("result",result);
        if(bindingResult.hasErrors()) {
            result.setMsg("操作失败");
        }
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BankFinanceController.verifyCancelForm"));
        return "bank/finance/verifyCancelForm";
    }

    @Token(TokenType.refresh)
    @PostMapping("/verifyCancel")
    public String verifyCancel(ModelMap model, Long id, @ModelAttribute("dto") @Valid FinanceVerifyCancelDTO dto, BindingResult result) {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "BankFinanceController.verifyCancel"));

        //需增已撤销验证
        if(result.hasErrors()) {
            return verifyCancelForm(model,id,dto,result);
        }
        UserPO currentUser = ShiroUtils.getCurrentUser();
        dto.setRevokeVerifyUserId(currentUser.getId());
        dto.setOptUserId(currentUser.getId());
        Integer count=service.updateVerifyCancel(id,XssUtils.replaceXss(dto));
        if(count==0){
            result.addError(new FieldError("dto", "global", "添加失败，请联系管理员"));
            return verifyCancelForm(model,id,dto,result);
        }

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BankFinanceController.verifyCancel"));
        return this.alertParentReload(model,"操作成功");
        //return alertLocation(model,"操作成功","./cancelDetail?id="+id);
    }

    @GetMapping("/cancelDetail")
    public String cancelDetail(ModelMap model,Long id,String aid) {
        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_BEGIN, "BankFinanceController.cancelDetail"));

        FinanceVO financeVO = service.getById(id);
        AuthPO authPO = authService.get(financeVO.getAuthId());
        
        if(!financeVO.getAid().equals(aid)){
            return this.errorAidNotMatch(model);
        }
        service.combo(financeVO);
        if(financeVO.getRevokeApplyUserId() != null) {
    		OrgPO orgPO = orgService.getOrgByUserId(financeVO.getRevokeApplyUserId());
        	
        	financeVO.setRevokeApplyUserName(orgPO.getOrgName());
        	
        	if("1".equals(financeVO.getRevokeVerifyStatus())) {
        		financeVO.setRevokeVerifyRemark("");
        	}
    	}else {
    		financeVO = new FinanceVO();
    	}
        financeVO.setRevokeType(authPO.getRevokeType());
        
        model.put("vo", financeVO);

        logger.info(TrcMsg.getMsg(TrcMsg.METHOD_END, "BankFinanceController.cancelDetail"));
        return "bank/finance/cancelDetail";
    }

}